﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Ccit.Utils;
using System.Collections;
using Ccit.Global;
using Ccit.OA.Utils;
using Ccit.OA.UserControls;
using Ccit.SysMsgRemind;
using Ccit.Mgoa.MsPsnset;
using Ccit.SysSMS;

namespace Ccit.Mgoa.Collaboration
{
    public class CollaborateSQL:ICollaborate
    {
        LoginUser loginUser = (LoginUser)HttpContext.Current.Session["LogUserInfo"];

        #region 根据条件获得协作类型

        public List<MsCollaborativeType> GetCollaborativeType(MsQuery msQuery)
        {
            List<MsCollaborativeType> lstCollaborativeType = new List<MsCollaborativeType>();
            string sSql = "SELECT * FROM CLB_A3 WHERE 1=1";
            if (msQuery != null)
            {
                string sQueryText = msQuery.QueryText;
                if (sQueryText != "")
                {
                    sQueryText = sQueryText.Replace("$$typeId$$", "CLB_A3_10");
                    sSql += sQueryText;
                }
            }
            sSql += " ORDER BY CLB_A3_9995 DESC";
            using (IDataReader iReader = DataAccessor.ExecuteReader(sSql))
            {
                while (iReader.Read())
                {
                    MsCollaborativeType msCollaborativeType = new MsCollaborativeType();
                    msCollaborativeType.TypeId = iReader["CLB_A3_10"].ToString();
                    msCollaborativeType.TypeName = iReader["CLB_A3_20"].ToString();
                    msCollaborativeType.TypeDesc = iReader["CLB_A3_30"].ToString();
                    msCollaborativeType.ManagementRange = iReader["CLB_A3_40"].ToString();
                    msCollaborativeType.ManagementId = iReader["CLB_A3_50"].ToString();
                    msCollaborativeType.PublishedRange = iReader["CLB_A3_60"].ToString();
                    msCollaborativeType.PublishedId = iReader["CLB_A3_70"].ToString();
                    msCollaborativeType.Creator = iReader["CLB_A3_9994"].ToString();
                    msCollaborativeType.CreateDate = iReader["CLB_A3_9995"].ToString();
                    lstCollaborativeType.Add(msCollaborativeType);
                }
            }
            return lstCollaborativeType;
        }

        #endregion

        #region 删除协作类型


        public bool DeleteCollaborativeType(string sTypeIds)
        {
            bool bFlag = false;
            string[] arrTypeId = sTypeIds.Split(',');
            ArrayList list = new ArrayList();
            string sSql = "";
            for (int i = 0; i < arrTypeId.Length; i++)
            {
                string sTypeId=arrTypeId[i];
                sSql = "DELETE FROM CLB_A3 WHERE CLB_A3_10='" + sTypeId + "'";
                list.Add(sSql);
            }
            if (DataAccessor.ExecuteNonQuery(list) > 0)
                bFlag = true;
            return bFlag;
        }

        #endregion

        #region 根据Id获得协作类型信息


        public MsCollaborativeType GetCollaborativeTypeById(string sTypeId)
        {
            MsQuery msQuery = new MsQuery();
            msQuery.QueryText = " AND $$typeId$$='" + sTypeId + "'";
            MsCollaborativeType msCollaborativeType = new MsCollaborativeType();
            List<MsCollaborativeType> lstCollaborativeType = GetCollaborativeType(msQuery);
            if(lstCollaborativeType.Count>0)
                msCollaborativeType = lstCollaborativeType[0];
            return msCollaborativeType;
        }

        #endregion

        #region 保存协作类型信息


        public string SaveCollaboration(MsCollaborativeType msCollaboration)
        {
            string sResult="";
            if (msCollaboration != null)
            {
                string[] sPubMsg = SessionExt.GetSysFieldSQL("CLB_A3");
                string sSql="";
                string sTypeId = msCollaboration.TypeId;
                string sTypeName = msCollaboration.TypeName;
                string sTypeDesc = msCollaboration.TypeDesc;
                string sManageRangeType = msCollaboration.ManagementRange;
                string sManageRangeId = msCollaboration.ManagementId;
                string sPublishRangeType = msCollaboration.PublishedRange;
                string sPublishRangeId = msCollaboration.PublishedId;
                if (sTypeId == "")
                {
                    sTypeId = DataInterface.GetMaxId("CLB_A3", "CLB_A3_10").ToString();
                    sSql = "INSERT INTO CLB_A3(CLB_A3_10,CLB_A3_20,CLB_A3_30,CLB_A3_40,CLB_A3_50,CLB_A3_60,CLB_A3_70," + sPubMsg[0] + ") VALUES (";
                    sSql += "'" + sTypeId + "','" + sTypeName.Replace("'", "''") + "','" + sTypeDesc.Replace("'", "''") + "','" + sManageRangeType + "','" + sManageRangeId + "','" + sPublishRangeType + "','" + sPublishRangeId + "'," + sPubMsg[1] + ")";
                }
                else
                {
                    sSql = "UPDATE CLB_A3 SET CLB_A3_20='" + sTypeName.Replace("'", "''") + "',CLB_A3_30='" + sTypeDesc.Replace("'", "''") + "',CLB_A3_40='" + sManageRangeType + "',CLB_A3_50='" + sManageRangeId + "',CLB_A3_60='" + sPublishRangeType + "'";
                    sSql += ",CLB_A3_70='" + sPublishRangeId + "' WHERE CLB_A3_10='" + sTypeId + "'";
                }
                if (DataAccessor.ExecuteNonQuery(sSql) > 0)
                    sResult = sTypeId;
            }
            return sResult;
        }

        #endregion

        #region 根据条件获得协作信息


        public List<MsCollaborativeTopic> GetCollaborativeTopicListByQuery(MsQuery msQuery)
        {
            DateTime CurDate=Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
            List<MsCollaborativeTopic> lstCollaborativeTopic = new List<MsCollaborativeTopic>();
            string sSql = "SELECT CLB_A1_10,CLB_A1_20,CLB_A1_30,CLB_A1_40,CLB_A1_50,CLB_A1_60,CLB_A1_70,CLB_A1_80,CLB_A1_90,CLB_A1_100,USERNAME FROM CLB_A1 INNER JOIN V_UDP ON CLB_A1_50=USERID WHERE 1=1";
            if (msQuery != null)
            {
                string sQueryText = msQuery.QueryText;
                if (sQueryText != "")
                {
                    sQueryText = sQueryText.Replace("$$title$$", "CLB_A1_20");
                    sQueryText = sQueryText.Replace("$$type$$", "CLB_A1_30");
                    sQueryText = sQueryText.Replace("$$status$$", "CLB_A1_100");
                    sQueryText = sQueryText.Replace("$$publishDate$$", "CLB_A1_60");
                    sQueryText = sQueryText.Replace("$$publisher$$", "USERNAME");
                    sSql += sQueryText;
                }
            }
            sSql += " ORDER BY CLB_A1_60 DESC";
            using (IDataReader iReader = DataAccessor.ExecuteReader(sSql))
            {
                while (iReader.Read())
                {
                    MsCollaborativeTopic msCollaborativeTopic = new MsCollaborativeTopic();
                    string sId=iReader["CLB_A1_10"].ToString();
                    msCollaborativeTopic.Id = sId;
                    msCollaborativeTopic.Title = iReader["CLB_A1_20"].ToString();

                    string sCollaborativeTypeId = iReader["CLB_A1_30"].ToString();
                    msCollaborativeTopic.TypeId = sCollaborativeTypeId;
                    msCollaborativeTopic.msCollaborativeType = GetCollaborativeTypeById(sCollaborativeTypeId);

                    msCollaborativeTopic.Content = iReader["CLB_A1_40"].ToString();
                    string sPublisherId = iReader["CLB_A1_50"].ToString();
                    msCollaborativeTopic.PublisherId = sPublisherId;
                    msCollaborativeTopic.PublisherName = SysInterface.GetNameStrByIdStr(sPublisherId, NameType.USER, ",");
                    string sPublishDate = iReader["CLB_A1_60"].ToString();
                    msCollaborativeTopic.PublishDate = sPublishDate;
                    string sFinishDate = iReader["CLB_A1_70"].ToString();
                    msCollaborativeTopic.FinishDate = sFinishDate;
                    msCollaborativeTopic.RangeType = iReader["CLB_A1_80"].ToString();
                    msCollaborativeTopic.RangeId = iReader["CLB_A1_90"].ToString();
                    string sStatus = iReader["CLB_A1_100"].ToString();
                    if ((CurDate - Convert.ToDateTime(sFinishDate)).Days > 0 && sStatus != "2")
                    {
                        sStatus = "2";
                        ChangeCollaborativeStatus(sId, "", true, "0");
                    }
                    msCollaborativeTopic.Status = sStatus;
                    lstCollaborativeTopic.Add(msCollaborativeTopic);
                }
            }

            string curOrganId = loginUser.OrganId;
            string curUserId = loginUser.UserId;
            string curDeptId = loginUser.DeptId;
            string curPosId = loginUser.PosId;
            string curClassId = loginUser.ClassId;
            StringLib oString;
            bool bCheck = false;

            IEnumerable<MsCollaborativeTopic> queryCol = lstCollaborativeTopic.Where(msg =>
            {
                oString = new StringLib(msg.RangeId);
                if (msg.PublisherId == curUserId||CheckHasPowerForType(msg.TypeId,"manage"))
                    bCheck = true;
                else
                {
                    bCheck = false;
                    switch (msg.RangeType)
                    {
                        case "0":
                            bCheck = oString.Contains(curUserId);//compareValue = curUserId;
                            break;
                        case "1":
                            bCheck = oString.Contains(curDeptId);//compareValue = curDeptId;
                            break;
                        case "2":
                            bCheck = oString.Contains(curPosId);//compareValue = curPosId;
                            break;
                        case "3":
                            bCheck = oString.Contains(curClassId);//compareValue = curClassId;
                            break;
                        case "8":
                            bCheck = oString.Contains(curOrganId);
                            break;
                        case "9":
                            bCheck = true;
                            break;
                        default:
                            bCheck = false;
                            break;
                    }
                }
                return bCheck;
            });

            lstCollaborativeTopic = queryCol.ToList();

            return lstCollaborativeTopic;
        }

        #endregion

        #region 获得当前用户权限范围内的协作类型


        public List<MsCollaborativeType> GetPowerRangeCollaborativeType(string sPowerType)
        {
            List<MsCollaborativeType> lstCollaborativeType = GetCollaborativeType(null);

            string sCurUserId = loginUser.UserId;
            string sCurOrgId = loginUser.OrganId;
            string sCurDeptId = loginUser.DeptId;
            string sCurClassId = loginUser.ClassId;
            string sCurPosId = loginUser.PosId;

            StringLib oString = null;
            bool bFind = false;
            IEnumerable<MsCollaborativeType> queryResult = null;

            if (sPowerType == "1") //管理权限
            {
                queryResult = lstCollaborativeType.Where(msg =>
                {
                    oString = new StringLib(msg.ManagementId);
                    switch (msg.ManagementRange)
                    {
                        case "0":
                            bFind = oString.Contains(sCurUserId);
                            break;
                        case "1":
                            bFind = oString.Contains(sCurDeptId);
                            break;
                        case "2":
                            bFind = oString.Contains(sCurPosId);
                            break;
                        case "3":
                            bFind = oString.Contains(sCurClassId);
                            break;
                        case "8":
                            bFind = oString.Contains(sCurOrgId);
                            break;
                        case "9":
                            bFind = true;
                            break;
                        default:
                            bFind = false;
                            break;
                    }
                    return bFind;
                });
            }
            else if (sPowerType == "2")  //发布权限
            {
                queryResult = lstCollaborativeType.Where(msg =>
                {
                    oString = new StringLib(msg.PublishedId);
                    switch (msg.PublishedRange)
                    {
                        case "0":
                            bFind = oString.Contains(sCurUserId);
                            break;
                        case "1":
                            bFind = oString.Contains(sCurDeptId);
                            break;
                        case "2":
                            bFind = oString.Contains(sCurPosId);
                            break;
                        case "3":
                            bFind = oString.Contains(sCurClassId);
                            break;
                        case "8":
                            bFind = oString.Contains(sCurOrgId);
                            break;
                        case "9":
                            bFind = true;
                            break;
                        default:
                            bFind = false;
                            break;
                    }
                    return bFind;
                });
            }
            lstCollaborativeType = queryResult.ToList();
            return lstCollaborativeType;
        }

        #endregion

        #region 更改协作状态 start：发布协作 restart：重启协作 stop：终止协作（手动）0：终止协作（日期过期自然终止）


        public bool ChangeCollaborativeStatus(string sId, string sFinishDate, bool bKeepCommInfo, string sOperType)
        {
            string sCurDate=DateTime.Now.ToString("yyyy-MM-dd");
            string sChangedStatus = "";
            ArrayList list = new ArrayList();
            string sSql = "UPDATE CLB_A1 SET CLB_A1_10='" + sId + "'";
            if (sOperType == "start")
            {
                sSql += ",CLB_A1_60='" + sCurDate + "'";
                sChangedStatus = "1";
            }
            else if (sOperType == "restart")
            {
                sSql += ",CLB_A1_70='" + sFinishDate + "'";
                sChangedStatus = "1";
            }
            else if (sOperType == "stop")
            {
                sSql += ",CLB_A1_70='" + sCurDate + "'";
                sChangedStatus = "2";
            }
            else if (sOperType == "0")
                sChangedStatus = "2";
            else
                sChangedStatus = "0";
            sSql += ",CLB_A1_100='" + sChangedStatus + "' WHERE CLB_A1_10='" + sId + "'";
            list.Add(sSql);
            if (!bKeepCommInfo)
            {
                DeleteCollaborateCommInfo(ref list, sId, bKeepCommInfo);
            }
            if (DataAccessor.ExecuteNonQuery(list) > 0)
                return true;
            return false;
        }

        #endregion

        /// <summary>
        /// 删除协作下的所有回复信息
        /// </summary>
        /// <param name="list"></param>
        /// <param name="sIds"></param>
        /// <param name="bKeepCommInfo"></param>
        public void DeleteCollaborateCommInfo(ref ArrayList list,string sIds, bool bKeepCommInfo)
        {
            if (!bKeepCommInfo)
            {
                string sSql = "";
                string[] arrId = sIds.Split(',');
                for (int i = 0; i < arrId.Length; i++)
                {
                    if (JudgeCollaborationCanBeDeleted(arrId[i]))
                    {
                        sSql = "DELETE FROM CLB_A2 WHERE CLB_A2_20='" + arrId[i] + "'";
                        list.Add(sSql);
                        DeleteAttachFromComment(ref list, arrId[i]);
                    }
                }
            }
        }

        /// <summary>
        /// 删除回复中的附件信息
        /// </summary>
        /// <param name="list"></param>
        /// <param name="sCollaborationId"></param>
        private void DeleteAttachFromComment(ref ArrayList list, string sCollaborationId)
        {
            string sSql = "";
            List<MsComment> lstComment = GetCollaborativeComment(sCollaborationId);
            AttachClass attach = new AttachClass();
            for (int i = 0; i < lstComment.Count; i++)
            {
                MsComment msComment = lstComment[i];
                sSql = attach.DeleteAttches("0050", msComment.Id);
                list.Add(sSql);
            }
        }

        #region 保存协作信息


        public string SaveCollaborativeTopic(MsCollaborativeTopic msCollaborativeTopic)
        {
            string sResult = "";
            ArrayList list = new ArrayList();
            if (msCollaborativeTopic != null)
            {
                string sSql = "";
                string sCurrDate=DateTime.Now.ToString("yyyy-MM-dd");
                string[] sPubMsg = SessionExt.GetSysFieldSQL("CLB_A1");
                string sId = msCollaborativeTopic.Id;
                string sTitle = msCollaborativeTopic.Title;
                string sTypeId = msCollaborativeTopic.TypeId;
                string sContent = msCollaborativeTopic.Content;
                string sPublisherId = msCollaborativeTopic.PublisherId;
                string sPuslishDate = msCollaborativeTopic.PublishDate;
                string sFinishDate = msCollaborativeTopic.FinishDate;
                string sRangeType = msCollaborativeTopic.RangeType;
                string sRangeId = msCollaborativeTopic.RangeId;
                bool bIsMsgRemind = msCollaborativeTopic.IsMsgRemind;
                bool bIsPhoneRemind = msCollaborativeTopic.IsPhoneRemind;
                string sStatus = "";
                //if (sCurrDate != sPuslishDate || sCurrDate != sFinishDate)
                //    sStatus = GetStatusByDate(sPuslishDate, sFinishDate);
                //else
                    sStatus = msCollaborativeTopic.Status;
                if (sId == "")
                {
                    sStatus = GetStatusByDate(sPuslishDate, sFinishDate);
                    sId = DataInterface.GetMaxId("CLB_A1", "CLB_A1_10").ToString();
                    sSql = "INSERT INTO CLB_A1(CLB_A1_10,CLB_A1_20,CLB_A1_30,CLB_A1_40,CLB_A1_50,CLB_A1_60,CLB_A1_70,CLB_A1_80,CLB_A1_90,CLB_A1_100," + sPubMsg[0] + ") VALUES (";
                    sSql += "'" + sId + "','" + sTitle.Replace("'", "''") + "','" + sTypeId + "','" + sContent.Replace("'", "''") + "','" + sPublisherId + "','" + sPuslishDate + "','" + sFinishDate + "','" + sRangeType + "',";
                    sSql += "'" + sRangeId + "','" + sStatus + "'," + sPubMsg[1] + ")";
                    if (bIsMsgRemind)
                    {
                        string sUserIds = SysInterface.GetUserIdsByRangeType(sRangeType, sRangeId, loginUser.OrganId);
                        string[] arrUserIds=sUserIds.Split(',');
                        sUserIds = "";
                        for (int i = 0; i < arrUserIds.Length; i++)
                        {
                            string sUserId = arrUserIds[i];
                            if (sUserIds == "")
                                sUserIds = sUserId + "_0_user";
                            else
                                sUserIds += "," + sUserId + "_0_user";
                        }
                        MsgRemindBLL oRemind = new MsgRemindBLL();
                        string sMsgContent = "请于" + sPuslishDate + "到" + sFinishDate + "参与关于\"" + sTitle + "\"的协作讨论。";
                        list=oRemind.GetSendMsgSQL(sMsgContent, sUserIds);
                    }
                    if (bIsPhoneRemind)
                    {
                        string sUserIds = SysInterface.GetUserIdsByRangeType(sRangeType, sRangeId, loginUser.OrganId);
                        string[] arrUserIds=sUserIds.Split(',');
                        sUserIds = "";
                        for (int i = 0; i < arrUserIds.Length; i++)
                        {
                            string sUserId = arrUserIds[i];
                            if (sUserIds == "")
                                sUserIds = sUserId + "_0_user";
                            else
                                sUserIds += "," + sUserId + "_0_user";
                        }
                        string sPhoneContent = "请于" + sPuslishDate + "到" + sFinishDate + "参与关于\"" + sTitle + "\"的协作讨论。";
                        SMSBLL oSms = new SMSBLL();
                        List<string> lstResult = oSms.SendSms(sPhoneContent,sUserIds,"0","0");
                    }
                    list.Add(sSql);
                }
                else
                {
                    sSql = "UPDATE CLB_A1 SET CLB_A1_20='" + sTitle.Replace("'", "''") + "',CLB_A1_30='" + sTypeId + "',CLB_A1_40='" + sContent.Replace("'", "''") + "',CLB_A1_60='" + sPuslishDate + "',CLB_A1_70='" + sFinishDate;
                    sSql += "',CLB_A1_80='" + sRangeType + "',CLB_A1_90='" + sRangeId + "',CLB_A1_100='" + sStatus + "' WHERE CLB_A1_10='" + sId + "'";
                    list.Add(sSql);
                }
                if (DataAccessor.ExecuteNonQuery(list) > 0)
                    sResult = sId;
            }
            return sResult;
        }

        public string GetStatusByDate(string sPublishDate, string sFinishDate)
        {
            string sStatus = "0";
            DateTime CurrDate = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
            DateTime PublishDate = Convert.ToDateTime(sPublishDate);
            DateTime FinishDate = Convert.ToDateTime(sFinishDate);
            TimeSpan tSpan1 = CurrDate - PublishDate;
            TimeSpan tSpan2 = CurrDate - FinishDate;
            TimeSpan tSpan3=FinishDate-PublishDate;
            if (tSpan1.Days > 0 && tSpan2.Days < 0) //当前日期>发布日期 and 当前日期 < 终止日期
                sStatus = "1";
            else if (tSpan2.Days > 0 && tSpan3.Days > 0)  //当前日期>发布日期 and 终止日期>发布日期
                sStatus = "2";
            else if (tSpan1.Days <= 0 && tSpan3.Days <= 0)   //当前日期<=发布日期 and 终止日期 <= 发布日期
                sStatus = "0";
            return sStatus;
        }

        #endregion

        #region 删除协作信息


        public bool DeleteCollaboration(string sIds)
        {
            ArrayList list = new ArrayList();
            string[] arrId = sIds.Split(',');
            string sSql = "";
            AttachClass attach = new AttachClass();
            string sCollaborationId = "";
            for (int i = 0; i < arrId.Length; i++)
            {
                sCollaborationId = arrId[i];
                if (JudgeCollaborationCanBeDeleted(sCollaborationId))
                {
                    sSql = "DELETE FROM CLB_A1 WHERE CLB_A1_10='" + sCollaborationId + "'";
                    list.Add(sSql);
                    sSql = attach.DeleteAttches("0040", sCollaborationId);
                    list.Add(sSql);
                }
            }
            attach = null;
            DeleteCollaborateCommInfo(ref list, sIds, false);
            if (DataAccessor.ExecuteNonQuery(list) > 0)
                return true;
            return false;
        }

        /// <summary>
        /// 判断当前人员是否可以删除该协作信息
        /// </summary>
        /// <param name="sCollaborationId"></param>
        /// <returns></returns>
        private bool JudgeCollaborationCanBeDeleted(string sCollaborationId)
        {
            MsCollaborativeTopic message = GetCollaborativeTopicById(sCollaborationId);
            if (loginUser.UserId == message.PublisherId || CheckHasPowerForType(message.TypeId, "manage"))
                return true;
            return false;
        }

        private bool CheckHasPowerForType(string sTypeId, string sPowerType)
        {
            bool bFlag = false;
            List<MsCollaborativeType> lstCollaborativeType = new List<MsCollaborativeType>();
            if (sPowerType.Equals("publish"))
            {
                lstCollaborativeType = GetPowerRangeCollaborativeType("2");
            }
            else if (sPowerType.Equals("manage"))
            {
                lstCollaborativeType = GetPowerRangeCollaborativeType("1");
            }

            if (lstCollaborativeType.Count > 0)
            {
                for (int i = 0; i < lstCollaborativeType.Count; i++)
                {
                    MsCollaborativeType message = lstCollaborativeType[i];
                    if (message.TypeId == sTypeId)
                    {
                        bFlag = true;
                        break;
                    }
                }
            }
            return bFlag;
        }

        #endregion

        #region 根据ID获得协作信息


        public MsCollaborativeTopic GetCollaborativeTopicById(string sId)
        {
            MsCollaborativeTopic message = new MsCollaborativeTopic();
            string sSql = "SELECT * FROM CLB_A1 WHERE CLB_A1_10='" + sId + "' ORDER BY CLB_A1_9995 DESC";
            using (IDataReader iReader = DataAccessor.ExecuteReader(sSql))
            {
                if (iReader.Read())
                {
                    message.Id = iReader["CLB_A1_10"].ToString();
                    message.Title = iReader["CLB_A1_20"].ToString();
                    message.TypeId = iReader["CLB_A1_30"].ToString();
                    message.Content = iReader["CLB_A1_40"].ToString();
                    message.PublisherId = iReader["CLB_A1_50"].ToString();
                    message.PublisherName = SysInterface.GetNameStrByIdStr(message.PublisherId, NameType.USER, ",");
                    message.PublishDate = iReader["CLB_A1_60"].ToString();
                    message.FinishDate = iReader["CLB_A1_70"].ToString();
                    message.RangeType = iReader["CLB_A1_80"].ToString();
                    message.RangeId = iReader["CLB_A1_90"].ToString();
                    message.Status = iReader["CLB_A1_100"].ToString();
                    message.CreateDate = iReader["CLB_A1_9995"].ToString();
                    message.lstComment = GetCollaborativeComment(sId);
                }
            }
            return message;
        }

        #endregion

        #region 根据协作ID获得其回复信息


        public List<MsComment> GetCollaborativeComment(string sCollaborativeId)
        {
            List<MsComment> lstComment = new List<MsComment>();
            string sSql = "SELECT * FROM CLB_A2 WHERE CLB_A2_20='" + sCollaborativeId + "' ORDER BY CLB_A2_50";
            using (IDataReader iReader = DataAccessor.ExecuteReader(sSql))
            {
                AttachClass attach = new AttachClass();
                while (iReader.Read())
                {
                    MsComment msComment = new MsComment();
                    msComment.Id = iReader["CLB_A2_10"].ToString();
                    msComment.CollaborativeId = iReader["CLB_A2_20"].ToString();
                    msComment.Content = iReader["CLB_A2_30"].ToString();
                    msComment.PublisherId = iReader["CLB_A2_40"].ToString();
                    msComment.PublisherName = SysInterface.GetNameStrByIdStr(msComment.PublisherId, NameType.USER, ",");
                    msComment.PublishDate = iReader["CLB_A2_50"].ToString();
                    msComment.HasAttaches = true;
                    lstComment.Add(msComment);
                }
                attach = null;
            }
            return lstComment;
        }

        #endregion

        #region 发表回复


        public string SaveComment(MsComment msComment)
        {
            string sResult = "";
            string sCollaborativeId = msComment.CollaborativeId;
            string sContent = msComment.Content;
            string sPublisherId = msComment.PublisherId;
            string sPublishDate = msComment.PublishDate;
            string sCommentId = DataInterface.GetMaxId("CLB_A2", "CLB_A2_10").ToString();
            string sSql = "INSERT INTO CLB_A2(CLB_A2_10,CLB_A2_20,CLB_A2_30,CLB_A2_40,CLB_A2_50) VALUES ('" + sCommentId + "','" + sCollaborativeId;
            sSql += "','" + sContent.Replace("'", "''") + "','" + sPublisherId + "','" + sPublishDate + "')";
            if (DataAccessor.ExecuteNonQuery(sSql) > 0)
                sResult = sCommentId;
            return sResult;
        }

        #endregion

        #region 根据ID获得评论详细信息


        public MsComment GetCommentById(string sCommentId)
        {
            MsComment msComment = new MsComment();
            string sSql = "SELECT * FROM CLB_A2 WHERE CLB_A2_10='" + sCommentId + "'";
            using (IDataReader iReader = DataAccessor.ExecuteReader(sSql))
            {
                if (iReader.Read())
                {
                    msComment.Id = iReader["CLB_A2_10"].ToString();
                    msComment.CollaborativeId = iReader["CLB_A2_20"].ToString();
                    msComment.Content = iReader["CLB_A2_30"].ToString();
                    msComment.PublisherId = iReader["CLB_A2_40"].ToString();
                    msComment.PublisherName = SysInterface.GetNameStrByIdStr(msComment.PublisherId, NameType.USER, ",");
                    msComment.PublishDate = iReader["CLB_A2_50"].ToString();
                }
            }
            return msComment;
        }

        #endregion

        #region 根据类别编号获得提醒设置


        public MsRemind GetRemindSetByTypeCode(string sTypeCode)
        {
            MsRemind msRemind = new MsRemind();
            string sCurUserId = loginUser.UserId;
            string sSql = "select * from MBO_B2 where MBO_B2_10='" + sCurUserId + "' and MBO_B2_20='" + sTypeCode + "'";
            using (IDataReader iReader = DataAccessor.ExecuteReader(sSql))
            {
                if (iReader.Read())
                {
                    msRemind.userid = iReader["MBO_B2_10"].ToString();
                    msRemind.typenum = iReader["MBO_B2_20"].ToString();
                    msRemind.mesflag = iReader["MBO_B2_30"].ToString();
                    msRemind.telflag = iReader["MBO_B2_40"].ToString();
                    msRemind.pageflag = iReader["MBO_B2_50"].ToString();
                }
            }
            return msRemind;
        }

        #endregion
    }
}
